package com.pn.config;

import com.pn.service.LoginUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/*
  Security配置类:
 */
@Configuration
public class SecurityLoginConfig extends WebSecurityConfigurerAdapter {

    /*
      1.认证配置:
     */

    //注入自定义的UserDetailsService的bean对象
    @Autowired
    private LoginUserDetailsService userDetailsService;

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    /*
      2.请求配置:
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //取消跨站跨域请求的检查
        http.csrf().disable();
        http.cors().disable();

        //对actuator的所有监控端点(url接口)直接放行
        http.authorizeRequests().antMatchers("/actuator/**").permitAll();

        //其它请求都必须登录后才可以访问
        http.authorizeRequests().anyRequest().authenticated();
    }

    /*
      3.配置认证管理器 -- 隐式登录
     */
    @Bean
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}
